<template>
  <div class="container">
    <div class="card">
      <div class="card2">
        <h2>调度管理系统</h2>
        <el-form
          :rules="rules"
          ref="ruleForm"
          :model="form"
          label-width="70px"
          label-position="left"
        >
          <el-form-item label="用户名" prop="name">
            <el-input
              v-model.trim="form.name"
              clearable
              prefix-icon="el-icon-user-solid"
            ></el-input> </el-form-item
          ><el-form-item label="密码" prop="password">
            <el-input
              v-model.trim="form.password"
              type="password"
              show-password
              clearable
              prefix-icon="el-icon-lollipop"
            ></el-input>
          </el-form-item>
          <div>
            <el-button type="primary" @click="register('ruleForm')"
              >注册</el-button
            >
            <el-button style="margin-left: 15px" @click="login('ruleForm')"
              >登录</el-button
            >
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Mock from 'mockjs';
import { setToken, removeToken } from '../utils/token';
export default {
  name: 'ManageForm',

  data() {
    return {
      form: {
        name: '',
        password: '',
      },
      rules: {
        name: [{ required: true, message: '请输入用户', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
        ],
      },
    };
  },

  mounted() {},

  methods: {
    register(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const token = Mock.Random.guid();
          this.$message.success('注册成功');
          this.$store.commit('role/SetUserInfo', this.form);
          this.$store.commit('token/setToken', token);
          setToken(token);
          this.$router.push('/main');
        }
      });
    },
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.quaryData();
          const token = Mock.Random.guid();
          this.$store.commit('token/setToken', token);
          setToken(token);
        } else {
          this.$message.error('请输入用户名和密码');
        }
      });
    },
    quaryData() {
      this.$http
        .login()
        .then((res) => {
          const reszult = res.data.find((i) => i.name == this.form['name']);
          if (reszult) {
            if (this.form['password'] == reszult['password']) {
              this.$message.success('登陆成功');
              this.$store.commit('role/SetUserInfo', this.form);
              this.$router.push('/main');
            } else {
              this.$message.error('密码错误');
            }
          } else {
            this.$message.error('该用户未注册！请注册');
          }
        })
        .catch((err) => {});
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .card {
    width: 450px;
    height: 390px;
    margin: 150px auto;
    background-image: linear-gradient(163deg, #00ff75 0%, #3700ff 100%);
    border-radius: 20px;
    transition: all 0.3s;
  }

  .card2 {
    width: 100%;
    height: 100%;
    border-radius: 20px;
    background: linear-gradient(357deg, #58626c 25%, #054066 75%);
    // background-color: #070d3b;
    transition: all 0.2s;
    display: flex;
    text-align: center;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    color: white;
    .el-form-item {
      width: 350px;
    }
  }

  .card2:hover {
    transform: scale(0.98);
    border-radius: 20px;
  }

  .card:hover {
    box-shadow: 0px 0px 30px 1px rgba(0, 255, 117, 0.3);
  }
}
</style>
